package com.yjz.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.InputFilter
import android.text.InputType
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.yjz.ui.R

/**
 *作用 : 输入栏
 *作者 : yjz
 *时间 : 2025/2/13 16:27
 */
@SuppressLint("CustomViewStyleable")
class InputItemLayout : LinearLayout {

    private var topLine: Line? = null
    private var bottomLine: Line? = null
    private var linePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private lateinit var inputEdt: EditText
    private lateinit var titleView: TextView

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attr: AttributeSet?) : this(context, attr, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(
        context,
        attributeSet,
        defStyle
    ) {

        orientation = HORIZONTAL
        linePaint.style = Paint.Style.FILL_AND_STROKE

        val styledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.InputItemLayout)

        // title
        val title = styledAttributes.getString(R.styleable.InputItemLayout_title)
        val titleRes = styledAttributes.getResourceId(R.styleable.InputItemLayout_titleTextAppearance, 0)
        paresTitleStyle(title, titleRes)

        // edittext
        val hint = styledAttributes.getString(R.styleable.InputItemLayout_hint)
        val maxInputLength = styledAttributes.getInteger(R.styleable.InputItemLayout_maxInputLength, 20)
        val inputType = styledAttributes.getInteger(R.styleable.InputItemLayout_inputType, 0)
        val inputRes = styledAttributes.getResourceId(R.styleable.InputItemLayout_inputTextAppearance, 0)
        paresInputStyle(hint, maxInputLength, inputType, inputRes)

        // line
        val topLineStyle = styledAttributes.getResourceId(R.styleable.InputItemLayout_topLineAppearance, -1)
        topLine = parseLineStyle(topLineStyle)

        val bottomLineStyle = styledAttributes.getResourceId(R.styleable.InputItemLayout_bottomLineAppearance, -1)
        bottomLine = parseLineStyle(bottomLineStyle)

        styledAttributes.recycle()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (topLine != null && topLine!!.enable) {
            linePaint.color = topLine!!.color
            linePaint.strokeWidth = topLine!!.height.toFloat()
            canvas.drawLine(topLine!!.leftMargin.toFloat(), 0f, measuredWidth - topLine!!.rightMargin.toFloat(), 0f, linePaint)
        }

        if (bottomLine != null && bottomLine!!.enable) {
            linePaint.color = bottomLine!!.color
            linePaint.strokeWidth = bottomLine!!.height.toFloat()
            canvas.drawLine(
                bottomLine!!.leftMargin.toFloat(),
                (height - bottomLine!!.height).toFloat(),
                width - bottomLine!!.rightMargin.toFloat(),
                (height - bottomLine!!.height).toFloat(),
                linePaint
            )
        }
    }

    /**
     * 解析上下边的线的样式
     */

    private fun parseLineStyle(lineRes: Int): Line? {
        if (lineRes == -1) return null
        val lineStyle = context.obtainStyledAttributes(lineRes, R.styleable.lineAppearance)
        val lineColor = lineStyle.getColor(R.styleable.lineAppearance_color, ContextCompat.getColor(context, R.color.color_d1d2))
        val lineHeight = lineStyle.getDimensionPixelSize(R.styleable.lineAppearance_height, 0)
        val leftMargin = lineStyle.getDimensionPixelSize(R.styleable.lineAppearance_leftMargin, 0)
        val rightMargin = lineStyle.getDimensionPixelSize(R.styleable.lineAppearance_rightMargin, 0)
        val enable = lineStyle.getBoolean(R.styleable.lineAppearance_enable, false)
        val line = Line()
        line.color = lineColor
        line.height = lineHeight
        line.leftMargin = leftMargin
        line.rightMargin = rightMargin
        line.enable = enable
        lineStyle.recycle()
        return line
    }

    /**
     * 解析输入框内容
     */
    private fun paresInputStyle(hint: String?, maxInputLength: Int, inputType: Int, inputRes: Int) {
        // 获取输入框的样式引用
        val inputStyle = context.obtainStyledAttributes(inputRes, R.styleable.inputTextAppearance)
        val inputHintColor = inputStyle.getColor(R.styleable.inputTextAppearance_hintColor, context.getColor(R.color.color_646))
        val inputColor = inputStyle.getColor(R.styleable.inputTextAppearance_inputColor, context.getColor(R.color.color_565))
        val inputSize = inputStyle.getDimensionPixelSize(R.styleable.inputTextAppearance_textSize, applyUnit(TypedValue.COMPLEX_UNIT_SP, 14f))

        inputEdt = EditText(context)
        // 去除style可能会带来的默认padding
        inputEdt.setPadding(0, 0, 0, 0)
        inputEdt.hint = hint
        inputEdt.setTextColor(inputColor)
        inputEdt.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(maxInputLength))
        when (inputType) {
            0 -> { // Text
                inputEdt.inputType = InputType.TYPE_CLASS_TEXT
            }

            1 -> { // password
                inputEdt.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            }

            2 -> { // number
                inputEdt.inputType = InputType.TYPE_CLASS_NUMBER
            }
        }
        inputEdt.setHintTextColor(inputHintColor)
        inputEdt.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputSize.toFloat())
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        layoutParams.weight = 1f
        inputEdt.layoutParams = layoutParams
        inputEdt.setPadding(0, 0, 0, 0)

        inputEdt.gravity = Gravity.LEFT or Gravity.CENTER_VERTICAL
        inputEdt.setBackgroundColor(Color.TRANSPARENT)
        addView(inputEdt)

        inputStyle.recycle()
    }

    /**
     * 解析左侧的title
     */
    @SuppressLint("CustomViewStyleable")
    private fun paresTitleStyle(title: String?, titleRes: Int) {

        val titleStyle = context.obtainStyledAttributes(titleRes, R.styleable.titleTextAppearance)
        val titleColor = titleStyle.getColor(R.styleable.titleTextAppearance_titleColor, ContextCompat.getColor(context, R.color.color_565))
        val titleSize = titleStyle.getDimensionPixelSize(R.styleable.titleTextAppearance_titleSize, applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f))
        val minWidth = titleStyle.getDimensionPixelOffset(R.styleable.titleTextAppearance_minWidth, 0)

        titleView = TextView(context)
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize.toFloat())  //sp---当做sp在转换一次
        titleView.setTextColor(titleColor)
        titleView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        titleView.minWidth = minWidth
        titleView.gravity = Gravity.LEFT or Gravity.CENTER_VERTICAL
        titleView.text = title
        addView(titleView)

        titleStyle.recycle()
    }

    class Line {
        var color: Int = 0
        var height: Int = 0
        var leftMargin: Int = 0
        var rightMargin: Int = 0
        var enable: Boolean = false
    }

    fun getEditText(): EditText {
        return inputEdt
    }

    fun getTitleText(): TextView {
        return titleView
    }

    fun getTextContent(): String {
        return inputEdt.text.toString()
    }

    private fun applyUnit(unit: Int, value: Float): Int {
        return TypedValue.applyDimension(unit, value, resources.displayMetrics).toInt()
    }

    fun setText(username: String?) {
        inputEdt.setText(username)
    }
}